package com.ty.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.annotation.Auth;
import com.ty.entity.Carts;
import com.ty.entity.Orders;
import com.ty.req.OrderReq;
import com.ty.resp.CommonResp;
import com.ty.service.IOrderItemService;
import com.ty.service.IOrdersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author txy
 * @since 2024-03-02
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Resource
    private IOrdersService ordersService;

    @GetMapping("/getOrders")
    @Auth(roles = "admin,user")
    public CommonResp getOrders(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "size", defaultValue = "5") Integer size,
                                @RequestParam(value = "search", required = false) String search,
                                @RequestParam(value = "userId", required = false) String userId,
                                @RequestParam(value = "status", required = false) Integer status) {
        return ordersService.getOrders(new Page(page, size), search, userId, status);
    }

    @PostMapping("/createOrder")
    @Auth(roles = "admin,user")
    public CommonResp createOrder(OrderReq orderReq) {
        return ordersService.createOrder(orderReq);
    }

    @PostMapping("/updateOrder")
    @Auth(roles = "admin,user")
    public CommonResp updateOrder(@RequestParam(value = "orderId") String orderId,
                                  @RequestParam(value = "paymentType",required = false) Integer paymentType,
                                  @RequestParam(value = "userId") String userId,
                                  @RequestParam(value = "status",required = false) Integer status) {
        return ordersService.updateOrder(orderId, paymentType,userId,status);
    }
    @PostMapping("/shipmentsOrder")//发货
    @Auth(roles = "admin")
    public CommonResp shipmentsOrder(Orders order) {
        return ordersService.shipmentsOrder(order);
    }
    @DeleteMapping("/deleteOrder")
    @Auth
    public CommonResp deleteOrder(@RequestParam(value = "orderId") String orderId,
                                  @RequestParam(value = "userId") String userId) {
        return ordersService.deleteOrder(orderId,userId);
    }
}
